<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改索引信息')" />
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-tableIndex-edit" th:object="${tableIndex}">
        <input name="id" th:field="*{id}" type="hidden">
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">索引名称：</label>
            <div class="col-sm-8">
                <input name="indexName" th:field="*{indexName}" class="form-control" type="text" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">索引英文名称：</label>
            <div class="col-sm-8">
                <input name="indexEnName" th:field="*{indexEnName}" class="form-control" type="text" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">索引类型：</label>
            <div class="col-sm-8">
                <select name="indexType" class="form-control m-b" th:with="type=${@dict.getType('indexType')}" required>
                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:field="*{indexType}"></option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">索引方法：</label>
            <div class="col-sm-8">
                <select name="indexFunction" class="form-control m-b" th:with="type=${@dict.getType('indexFunction')}" required>
                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:field="*{indexFunction}"></option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">注释：</label>
            <div class="col-sm-8">
                <input name="remark" th:field="*{remark}" class="form-control" type="text">
            </div>
        </div>
        <h4 class="form-header h4">字段信息信息</h4>
        <div class="row">
            <div class="col-sm-12">
                <button type="button" class="btn btn-white btn-sm" onclick="addColumn()"><i class="fa fa-plus"> 增加</i></button>
                <button type="button" class="btn btn-white btn-sm" onclick="sub.delColumn()"><i class="fa fa-minus"> 删除</i></button>
                <div class="col-sm-12 select-table table-striped">
                    <table id="bootstrap-table"></table>
                </div>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer" />
<script th:inline="javascript">
    var prefix = ctx + "tableIndex/tableIndex";
    var fieldInfoDatas = [[${fieldInfoDatas}]];
    $("#form-tableIndex-edit").validate({
        focusCleanup: true
    });

    function submitHandler() {
        if ($.validate.form()) {
            $.operate.save(prefix + "/edit", $('#form-tableIndex-edit').serialize());
        }
    }

    $(function () {
        var options = {
            data: [[${tableIndex.tableIndexFieldList}]],
            pagination: false,
            showSearch: false,
            showRefresh: false,
            showToggle: false,
            showColumns: false,
            sidePagination: "client",
            columns: [{
                checkbox: true
            },
                {
                    field: 'index',
                    align: 'center',
                    title: "序号",
                    width:50,
                    formatter: function (value, row, index) {
                        var columnIndex = $.common.sprintf("<input type='hidden' name='index' value='%s'>", $.table.serialNumber(index));
                        return columnIndex + $.table.serialNumber(index);
                    }
                },
                {
                    field: 'fieldId',
                    align: 'center',
                    title: '字段',
                    width:250,
                    formatter: function(value, row, index) {
                        var name = $.common.sprintf("tableIndexFieldList[%s].fieldId", index);
                        return dictToSelect(fieldInfoDatas, value, name);
                    }
                },
                {
                    field: 'orderNum',
                    align: 'center',
                    title: '排序',
                    width:100,
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control' type='number' name='tableIndexFieldList[%s].orderNum' value='%s'>", index, value);
                        return html;
                    }
                }]
        };
        $.table.init(options);
    })

    function dictToSelect (datas, value, name, className) {
        className = className || '';
        var actions = [];
        actions.push($.common.sprintf("<select class='form-control %s' name='%s'>", className, name));
        $.each(datas, function(index, dict) {
            actions.push($.common.sprintf("<option value='%s'", dict.id));
            if (dict.id == value) {
                actions.push(' selected');
            }
            actions.push($.common.sprintf(">%s</option>", dict.fieldName));
        });
        actions.push('</select>');
        return actions.join('');
    }

    function addColumn() {
        var count = $("#" + table.options.id).bootstrapTable('getData').length;
        sub.editColumn();
        $("#" + table.options.id).bootstrapTable('insertRow', {
            index: count,
            row: {
                index: $.table.serialNumber(count),
                fieldId: "",
                orderNum: ""
            }
        });
    }
</script>
</body>
</html>